const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
const FORM = 'FORM';
const DELETE = 'DELETE';

const baseURL = 'http://localhost:3000/';

function request(method, url, data) {
    return new Promise(function (resolve, reject) {
        let header = {
            'content-type': 'application/json',
        };
        wx.request({
            url: baseURL + url,
            method: method,
            data: method === POST ? JSON.stringify(data) : data,
            header: header,
            success(res) {
                //请求成功
                //判断状态码---errCode状态根据后端定义来判断
                if (res.data.code === 200) {
                    resolve(res);
                } else {
                    //其他异常
                    reject('运行时错误,请稍后再试');
                }
            },
            fail(err) {
                //请求失败
                reject(err)
            }
        })
    })
}

const API = {
    // home
    getOrderList: () => request(GET, 'order/order_list'),
    loadImg: () => request(GET, 'order/order_bg'),

    // my
    login: (data) => request(GET, `user/login?code=${data}`),
    getUserinfo: (data) => request(POST, 'user/userinfo', data),
    getPublish: (data) => request(POST, 'order/order_publish', data),
    getCollect: (data) => request(POST, 'collect/order_collect', data),
    getTaking: (data) => request(POST, 'take/order_take', data),

    // orderDetail
    loadPic: (data) => request(POST, 'order/detail_pic', data),
    loadMsg: (data) => request(POST, 'order/msg_board', data),
    isCollect: (data) => request(POST, 'collect/isCollect', data),
    addCollect: (data) => request(POST, 'collect/addCollect', data),
    delCollect: (data) => request(POST, 'collect/delCollect', data),
    takeOrder: (data) => request(POST, 'take/takeOrder', data),
    isTake: (data) => request(POST, 'take/isTake', data),
    getProcess: (data) => request(GET, `order/order_process?order_id=${data}`),
    updateState: (data) => request(POST, 'order/updateState', data),
    addMsg: (data) => request(POST, 'order/addMsg', data),

    // rate
    rate: (data) => request(POST, 'user/rate', data),

    // userinfo
    updateUser: (data) => request(POST, 'user/updateUser', data),

    // publish
    addOrder: (data) => request(POST, 'order/addOrder', data),
    
    // complain
    addComplain: (data) => request(POST, 'complain/addComplain', data)

};
module.exports = {
    API: API
}